﻿using System;
using System.Collections.Generic;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageK.Util
{
    /** This class contains static utility methods for replacing fonts that are not available on the
     *  current system.
     */

    public class FontUtil
    {

        /** Returns a font with the given family name or, if not available, a similar font, e.g. Helvetica replaced by Arial */
        public static Font getFont(String fontFamilyName, int style, float size)
        {
            // Font font = new Font(fontFamilyName, style, (int)size);
            Font font = new Font(new FontFamily(fontFamilyName), size);
            if (!font.FontFamily.Name.StartsWith(fontFamilyName))
            {
                String[] similarFonts = getSimilarFontsList(fontFamilyName);
                font = getFont(similarFonts, style, (int)size);
            }
            if (size != (int)size)
                font = font.deriveFont(size);
            return font;
        }

        /** Returns the font for first element of the 'fontNames' array, where a Font Family Name starts with this name.
         *	E.g. if fontNames = {"Times New Roman", "Serif" and the system has no "Times New Roman", but finds "Serif",
         *	it would return a "Serif" font with suitable style and size */
        private static Font getFont(String[] fontNames, int style, int size)
        {
            int iSize = (int)size;
            Font font = null;
            foreach (String fontName in fontNames)
            {
                font = new Font(new FontFamily(fontName), iSize);
                // font = new Font(fontName, style, iSize);
                if (font.FontFamily.Name.StartsWith(fontName))
                    break;
            }
            return font;
        }

        /** For a few basic font types, gets a list of replacement font families
         *	Note that java's 'SansSerif' has wider characters (significantly different metrics) than the other
         *	sans-serif fonts in the list; thus it should be considered a fallback option only.
         *	Also note that some fonts (Times, Helvetica, Courier, Monospace, Serif) tend to truncate some
         *	diacritical marks when using FontMetrics.getHeight (as ImageJ does), e.g. the ring of the
         *  Angstrom symbol Å may be clipped (at least on Java 1.6/Mac)
         */
        public static String[] getSimilarFontsList(String fontFamily)
        {
            if (fontFamily.IndexOf("Times")>=0 || fontFamily.IndexOf("Serif")>=0)
                return new String[] { "Times New Roman", "Times", "Liberation Serif", "Serif" };
            else if (fontFamily.IndexOf("Arial")>=0 || fontFamily.IndexOf("Helvetica")>=0 || fontFamily.IndexOf("Sans")>=0)
                return new String[] { "Arial", "Helvetica", "Helvetica Neue", "Liberation Sans", "SansSerif" };
            else if (fontFamily.IndexOf("Courier")>=0 || fontFamily.IndexOf("Mono")>=0)
                return new String[] { "Courier New", "Courier", "Liberation Mono", "Monospaced" };
            else
                return new String[] { fontFamily };
        }


    }

}
